package demoApiOfKboot.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.*;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.*;

import com.kayak.exception.ErrorException;
import com.kayak.kboot.dao.BaseDao;
import com.kayak.kboot.dao.JedisEx;
import com.kayak.kboot.dao.KTransaction;
import com.kayak.kboot.dao.SqlManager;
import com.kayak.kboot.dao.kSql.SqlStatement;



/**
 *各种测试场景
 * @author caix
 *
 */
@RestController
public class DemoAction {
	
	@Autowired@Qualifier("comnDao")
	BaseDao comnDao;
	@Autowired
	JedisEx jedisEx;
	
	@Autowired@Qualifier("secondDao")
	BaseDao secondDao;
	
	@Autowired@Qualifier("thirdDao")
	BaseDao thirdDao;
	
	Map transactionMap;
	
	
	
	
	
	
	
    @RequestMapping("/demo-dbupdate")
    String demo_dbupdate() {
    	try {
    		Map dbMap=new HashMap<String, Object>();
    		dbMap.put("id1", UUID.randomUUID().toString().replace("-", ""));
			this.comnDao.update("test1",dbMap);
		} catch (ErrorException e) {
			e.printStackTrace();
		}
        return "Hello dbupdate!";
    }
    
    @RequestMapping("/demo-jedisEx")
    String demo_jedisEx() {
    	jedisEx.setex("kboot-first_redis-key", 5, "kboot-first_redis-value");
    	try {
			Thread.sleep(1001);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	System.out.println("try first time :"+jedisEx.get("kboot-first_redis-key"));
    	try {
			Thread.sleep(5001);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	System.out.println("try second time :"+jedisEx.get("kboot-first_redis-key"));
    	return  "hello jedisEx";
    }
    
    
    
    @RequestMapping("/demo-batchsql-transaction")
    String demo_dbtransaction() {
    	
    	Map map =new HashMap<String, Object>();
    	List<Map> list=new ArrayList<Map>();
    	for(int i=0;i<3215;i++){
    		Map temp = new HashMap<String, Object>();
    		temp.put("id", i);
    		list.add(temp);
    	}
    	map.put("list", list);
    	
    	try {
    		new KTransaction(secondDao,map) {
    			@Override
    			protected void inTransaction(TransactionStatus status) throws Exception{
    					SqlStatement stm =this.getDao().newSqlStatement(SqlManager.getSqlid("batch1").getSql());
    					stm.batchUpdateInITransaction((List<Map<String, Object>>) this.getParams().get("list"),false);
    			}
    		}.execute();
    		
    		new KTransaction(comnDao,map) {
    			@Override
    			protected void inTransaction(TransactionStatus status) throws Exception{
    					SqlStatement stm =this.getDao().newSqlStatement(SqlManager.getSqlid("batch1").getSql());
    					stm.batchUpdateInITransaction((List<Map<String, Object>>) this.getParams().get("list"),false);
    			}
    		}.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
		
    		
    		
        return "Hello batchsql-transaction!";
    }
    
    
    
    @RequestMapping("/demo-batchsql-3dao")
    String demo_3dao() {
    	
    	Map map =new HashMap<String, Object>();
    	List<Map> list=new ArrayList<Map>();
    	for(int i=0;i<10;i++){
    		Map temp = new HashMap<String, Object>();
    		temp.put("id", i);
    		list.add(temp);
    	}
    	map.put("list", list);
    	
    	try {
    		
    		//mysql
    		new KTransaction(comnDao,map) {
    			@Override
    			protected void inTransaction(TransactionStatus status) throws Exception{
    					SqlStatement stm =this.getDao().newSqlStatement(SqlManager.getSqlid("batch1").getSql());
    					stm.batchUpdateInITransaction((List<Map<String, Object>>) this.getParams().get("list"),false);
    			}
    		}.execute();
    		
    		//oracle
    		new KTransaction(secondDao,map) {
    			@Override
    			protected void inTransaction(TransactionStatus status) throws Exception{
    					SqlStatement stm =this.getDao().newSqlStatement(SqlManager.getSqlid("batch1").getSql());
    					stm.batchUpdateInITransaction((List<Map<String, Object>>) this.getParams().get("list"),false);
    			}
    		}.execute();
    		//mysql
    		new KTransaction(thirdDao,map) {
    			@Override
    			protected void inTransaction(TransactionStatus status) throws Exception{
    					SqlStatement stm =this.getDao().newSqlStatement(SqlManager.getSqlid("batch1").getSql());
    					stm.batchUpdateInITransaction((List<Map<String, Object>>) this.getParams().get("list"),false);
    			}
    		}.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
		
    		
    		
        return "Hello batchsql-transaction!";
    }
    
    
    
    @RequestMapping("/demo-differentsqls")
    String demo_differentsqls() {
    	

        	Map dbMap=new HashMap<String, Object>();
    		dbMap.put("id1", UUID.randomUUID().toString().replace("-", ""));
    		dbMap.put("id2", UUID.randomUUID().toString().replace("-", ""));
    		dbMap.put("id3", UUID.randomUUID().toString().replace("-", ""));
        	
    		try {
    			new KTransaction(secondDao,dbMap) {
        			@Override
        			protected void inTransaction(TransactionStatus status) throws Exception{
        				SqlStatement stm1 =this.getDao().newSqlStatement(SqlManager.getSqlid("test1").getSql());
        				stm1.autoSetParams(this.getParams());
        				stm1.executeUpdate();
        				
        				SqlStatement stm2 =this.getDao().newSqlStatement(SqlManager.getSqlid("test2").getSql());
        				stm2.autoSetParams(this.getParams());
        				int i=stm2.executeUpdate();
//        				if(i>0){
//        					throw new ErrorException("人为退出测试");
//        				}
        				SqlStatement stm3 =this.getDao().newSqlStatement(SqlManager.getSqlid("test3").getSql());
        				stm3.autoSetParams(this.getParams());
        				stm3.executeUpdate();
        			}
        		}.execute();
				
			} catch (Exception e) {
				e.printStackTrace();
				 return "Hello demo_differentsqls error!";
			}
    		
    		
        
    	
        return "Hello demo_differentsqls success!";
    }
    
    
    
    @RequestMapping("/demo-comnDao")
    String demo_seconddb() {
    	try {
    		Map<String, Object> dbMap=new HashMap<String, Object>();
    		dbMap.put("id1", UUID.randomUUID().toString().replace("-", ""));
			this.comnDao.update("test1",dbMap);
			
		} catch (ErrorException e) {
			e.printStackTrace();
		}
        return "Hello comnDao!";
    }
    
    @RequestMapping("/demo-twodbchange")
    String demo_twodbchange() {
    	try {
    		Map dbMap=new HashMap<String, Object>();
    		dbMap.put("id1", UUID.randomUUID().toString().replace("-", ""));
    		this.secondDao.update("test1",dbMap);
    		this.comnDao.update("test1",dbMap);
			this.comnDao.update("test1",dbMap);
			this.comnDao.update("test1",dbMap);
			this.comnDao.update("test1",dbMap);
			this.comnDao.update("test1",dbMap);
			this.comnDao.update("test1",dbMap);
			this.secondDao.update("test1",dbMap);
			this.secondDao.update("test1",dbMap);
		} catch (ErrorException e) {
			e.printStackTrace();
		}
        return "Hello twodbchange!";
    }
    
    
    
    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(DemoAction.class, args);
    }
}